/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.ejb;

import com.mycompany.psc.controllers.ProjectsBean;
import com.mycompany.psc.data.TenderType;
import com.mycompany.psc.entities.Aluminium;
import com.mycompany.psc.entities.Carpenter;
import com.mycompany.psc.entities.MetalDoors;
import com.mycompany.psc.entities.Mobilization;
import com.mycompany.psc.entities.Projects;
import com.mycompany.psc.entities.SiteExpenses;
import com.mycompany.psc.entities.TenderItems;
import com.mycompany.psc.entities.Users;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author A7med
 */
@Stateless
public class ReportableItemsManager {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext(unitName = "com.mycompany_PSC_war_1.0-SNAPSHOTPU")
    EntityManager em;
    @Inject
    ProjectsBean pb;

    public List<TenderItems> getResources(TenderType type, Projects p) {
        TypedQuery<TenderItems> query = em.createNamedQuery("TenderItems.findAllInProject", TenderItems.class);
        query.setParameter("type", type);
        query.setParameter("project", p);
        return query.getResultList();
    }
    public Object getResource(String queryString, Projects p,String code) {
        Query query = em.createNamedQuery(queryString);
        query.setParameter("code", code);
        query.setParameter("project", p);
        return query.getResultList().get(0);
    }

    public List<Mobilization> getMobilizationResources(Projects p) {
        TypedQuery<Mobilization> query = em.createNamedQuery("Mobilization.findByProject", Mobilization.class);
        query.setParameter("project", p);
        return query.getResultList();
    }

    public List<Aluminium> getAluminiumResources(Projects p) {
        TypedQuery<Aluminium> query = em.createNamedQuery("Aluminium.findByProject", Aluminium.class);
        query.setParameter("project", p);
        return query.getResultList();
    }

    public List<MetalDoors> getSteelDoorsResources(Projects p) {
        TypedQuery<MetalDoors> query = em.createNamedQuery("MetalDoors.findByProject", MetalDoors.class);
        query.setParameter("project", p);
        return query.getResultList();
    }

    public List<Carpenter> getCarpenterResources(Projects p) {
        TypedQuery<Carpenter> query = em.createNamedQuery("Carpenter.findByProject", Carpenter.class);
        query.setParameter("project", p);
        return query.getResultList();
    }

    public List<SiteExpenses> getSiteExpensesResources(Projects p) {
        TypedQuery<SiteExpenses> query = em.createNamedQuery("SiteExpenses.findByProject", SiteExpenses.class);
        query.setParameter("project", p);
        return query.getResultList();
    }

    public TenderItems getResourceByName(String name) {
        TypedQuery<TenderItems> query = em.createNamedQuery("TenderItems.findByTitleAndProject", TenderItems.class);
        query.setParameter("title", name);
        query.setParameter("project", pb.getSelectedReportProject());
        return query.getSingleResult();
    }

    public Object getResourceByName(String queryName, String key, String value) {
        Query query = em.createNamedQuery(queryName);
        query.setParameter(key, value);
        query.setParameter("project", pb.getSelectedReportProject());
        System.out.println("gettig data"+key+":"+value);
        System.out.println("gettig out"+query.getResultList().get(0).toString());
        return query.getResultList().get(0);
    }
     public Object getResourceById(String queryName, String id) {
        Query query = em.createNamedQuery(queryName);
        query.setParameter("id", Integer.parseInt(id));
        return query.getResultList().get(0);
    }

    public Mobilization getMobilizationResourceByName(String name) {
        TypedQuery<Mobilization> query = em.createNamedQuery("Mobilization.findByTitleAndProject", Mobilization.class);
        query.setParameter("title", name);
        query.setParameter("project", pb.getSelectedReportProject());
        return query.getResultList().get(0);
    }

    public Aluminium getAluminiumResourceByName(String name) {
        TypedQuery<Aluminium> query = em.createNamedQuery("Aluminium.findByTitleAndProject", Aluminium.class);
        query.setParameter("title", name);
        query.setParameter("project", pb.getSelectedReportProject());
        return query.getResultList().get(0);
    }

    public void save(TenderType type, Projects p, Users u, String title, float expense, int amount,String code) {
        TenderItems t = new TenderItems();
        t.setAuthor(u.getName());
        t.setProject(p);
        t.setSubmitDate(new Date());
        t.setTitle(title);
        t.setExpences(expense);
        t.setAmount(amount);
        t.setType(type);
        t.setCode(code);
        em.persist(t);
    }

    public void saveMobilization(Projects p, Users u, String title,String code) {
        Mobilization t = new Mobilization();
        t.setAuthor(u.getName());
        t.setProject(p);
        t.setSubmitDate(new Date());
        t.setTitle(title);
t.setCode(code);
        em.persist(t);
    }

    public void saveAluminium(Projects p, Users u, String title, String dim, int quantity, float rate, double aluRate,String code) {
        Aluminium t = new Aluminium();
        t.setAuthor(u.getName());
        t.setProject(p);
        t.setSubmitDate(new Date());
        t.setDimension(dim);
        t.setQuantity(quantity);
        t.setRate(rate);
        t.setAluRate(aluRate);
        t.setCode(code);
        t.setTitle(title);

        em.persist(t);
    }

    public void persistObject(Object o) {
        em.persist(o);
    }

    public void delete(TenderItems t) {
        TenderItems deletable = em.getReference(TenderItems.class, t.getItemId());
        em.remove(deletable);
    }

    public void update(Object t) {
        //TenderItems updatable=em.getReference(TenderItems.class, t.getItemId());
        em.merge(t);
    }
}
